const App = getApp();
const wxParse = require('../../wxParse/wxParse.js')
const qingxilist = require('../../utils/qingxilist.js')
const util = require('../../utils/util.js')
const siteinfo = require('../../siteinfo.js')

Page({

  data: {
    // 页面参数
    qingxilist: qingxilist,
    util: util,
    options: {},
    // 页面元素
    scrollTop: 0,
    index: 0,
    detail: {},
    qingkong: '',
    $data: {},
    qingkongList: [], //搜索的机构的list
    jieDaoId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    let arr = {
      ..._this.data
    }
    delete arr.$data
    this.setData({
      $data: arr
    })
    let curdate = util.dateFormat('YYYY-mm-dd', new Date())
    this.setData({
      TianBiaoRiQi: curdate,
      jieDaoId:options.jieDaoId
    })
    console.log("街道Id",this.data.jieDaoId)
  },
  formSubmit(e) {
    var _this = this
    let data = {
      ...e.detail.value,
    }
    data.jieDaoId = getApp().globalData.jieDaoId
    try {
      if (_this.data.loading) {
        return
      }
      _this.setData({
        loading: true
      })
    } catch (e) {
      wx.showModal({
        cancelColor: 'cancelColor',
        showCancel: false,
        title: '错误',
        content: 'e'
      })
    }
    wx.showLoading({
      title: '提交中',
    })
    // return
    wx.request({
      url: App.api_test + 'SaveChengZhongCunBase',
      header: {
        'content-type': 'application/JSON',
      },
      method: 'POST',
      data: {
        baseEntity:data,
        openId:wx.getStorageSync('openid'),
        userId:wx.getStorageSync('user_id')
      },
      success(res){
        _this.setData(_this.data.$data)
        wx.showModal({
          title: res.data,
          showCancel: false,
          complete: () => {
            _this.setData({
              loading: false
            })
          }
        })
      },
      fail(res) {
        console.log(res);
      }
    })
    wx.hideLoading()
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    let _this = this;
    return {
      title: _this.data.detail.Name,
      path: `/pages/xiaoqu/idnex`,
    };
  },
  shanchu(e) {
    //  e.target.name
    let _this = this
    console.log(e)
    let arr = []
    arr = arr.concat(_this.data[e.target.dataset.name])
    arr.splice(e.target.dataset.index, 1)
    this.setData(_this.setKeyAndValue(e.target.dataset.name, arr))
  },
  setKeyAndValue(key, value) {
    var obj = {};
    console.log(key)
    obj[key] = value
    console.log(obj)
    return obj
  },
  searchQingkong(e) {
    let _this = this;
    App._get('GetQingXiJiGou', {
      name: e.detail.value
    }, (res) => {
      console.log(res)
      if (res.length === 0) {
        res = [{
          Name: '暂无数据'
        }]
      }
      _this.setData({
        qingkongList: res
      })
    })
  },
  selectQingkong(e) {
    this.setData({
      qingkongList: [],
      ErGongSheShiQingXiJiGou: e.currentTarget.dataset.name
    })
  },
  shanchu(e) {
    //  e.target.name
    let _this = this
    console.log(e)
    let arr = []
    arr = arr.concat(_this.data[e.target.dataset.name])
    arr.splice(e.target.dataset.index, 1)
    this.setData(_this.setKeyAndValue(e.target.dataset.name, arr))
  },
  setKeyAndValue(key, value) {
    var obj = {};
    console.log(key)
    obj[key] = value
    console.log(obj)
    return obj
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    // 获取首页数据
    wx.stopPullDownRefresh();
  },
  /**
   * 滚动到底部
   */
  goBottom() {
    var obj = wx.createSelectorQuery();
    obj.selectAll('.container').boundingClientRect(function (rect) {
      wx.pageScrollTo({
        scrollTop: rect[0].height,
        duration: 300
      });
    })
    obj.exec();
  },
  onShareAppMessage() {
    return {
      title: '城中村巡查',
      path: "/pages/chengzhongcun/index"
    };
  },

});